package org.itboys.mobile.service.mongo.front;

import org.apache.commons.lang3.StringUtils;
import org.itboys.commons.utils.tools.ListUtil;
import org.itboys.mobile.common.SundayCommonConstants;
import org.itboys.mobile.dto.SundayAddressDto;
import org.itboys.mobile.entity.mongo.SundayAddress;
import org.itboys.mobile.entity.mongo.SundayMember;
import org.itboys.mobile.service.mongo.web.SundayMemberService;
import org.itboys.mongodb.core.MongoDataSource;
import org.itboys.mongodb.service.BaseMongoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * 作者：jiangxiong
 * 日期：2017年5月8日
 * 联系方式 ：empty90911@126.com
 * 描述：圣代_收获地址_service_移动端
 */
@Service
public class SundayFrontAddressService extends BaseMongoService<SundayAddress> {
    @Autowired
    private SundayMemberService memberService;
    @Resource(name = "mobileDs")
    private MongoDataSource sundayDataSource;
    @Override
    protected MongoDataSource getMongoDataSource() {
        return sundayDataSource;
    }

    @Override
    protected Class<SundayAddress> getEntityClass() {
        return SundayAddress.class;
    }

    /**
     * 获取用户所有收货地址
     * @param memberId
     * @return
     */
    public List<SundayAddressDto> select(Long memberId){
        Map<String,Object> param =new HashMap<String, Object>();
        param.put("memberId", memberId);
        param.put("isDeleted", SundayCommonConstants.NO);
        //有限排序默认地址
        param.put("sort","isDefault");
        param.put("order","DESC");
        //SundayMember member=memberService.getById(memberId);
        List<SundayAddressDto> list = packageDtoMuilt(super.list(param));
        return list;
    }

    /**
     * 组装dto(多)
     * @param adds
     * @return
     */
    public List<SundayAddressDto> packageDtoMuilt(List<SundayAddress> adds){
        List<SundayAddressDto> dtoList = new ArrayList<SundayAddressDto>();
        for(SundayAddress address:adds){
            SundayAddressDto dto = packageDtoSingle(address);
            dtoList.add(dto);
        }
        return dtoList;
    }

    /**
     * 组装dto(单)
     * @param address
     * @return
     */
    public SundayAddressDto packageDtoSingle(SundayAddress address){
        SundayAddressDto dto = new SundayAddressDto();
        dto.setId(address.getId());
        dto.setCityId(address.getCityId());
        dto.setCityName(address.getCityName());
        dto.setDistrictId(address.getDistrictId());
        dto.setDistrictName(address.getDistrictName());
        dto.setIsDefault(address.getIsDefault());
        dto.setMemberId(address.getMemberId());
        dto.setProvinceId(address.getProvinceId());
        dto.setProvinceName(address.getProvinceName());
        dto.setReceiveName(address.getReceiveName());
        dto.setReceiveMobile(address.getReceiveMobile());
        dto.setReceiveAddress(address.getReceiveAddress());
        dto.setReceiveFullAddress(address.getCityName()+address.getProvinceName()+address.getDistrictName()+address.getReceiveAddress());
        /*if(StringUtils.isNotBlank(address.getCityName()) && StringUtils.isNotBlank(address.getProvinceName())){
            if(address.getCityName().equals(address.getProvinceName())){//直辖市
                dto.setReceiveAddress(address.getProvinceName()+address.getDistrictName()+address.getReceiveAddress());
            }else{
                dto.setReceiveAddress(address.getCityName()+address.getProvinceName()+address.getDistrictName()+address.getReceiveAddress());
            }
        }*/

        return dto;
    }

    /**
     * 获取详情地址dto
     * @param id
     * @return
     */
    public SundayAddressDto getDetailAddress(Long id){
        SundayAddress address = new SundayAddress();
        if(id == null || id == 0L){
            address.setId(0);
            address.setIsDefault(1);
        }else{
            address = super.getById(id);
        }
        return packageDtoSingle(address);
    }



    /**
     * 新增收货地址
     * @param id
     * @param memberId
     * @param receiveName
     * @param receiveMobile
     * @param provinceId
     * @param provinceName
     * @param cityId
     * @param cityName
     * @param districtId
     * @param districtName
     * @param receiveAddress
     */
    public void save(Long id, Long memberId, String receiveName, String receiveMobile,
                     Long provinceId, String provinceName, Long cityId, String cityName, Long districtId, String districtName,
                     String receiveAddress){



        if(id==null||id==0l){
            Map<String,Object> param = new HashMap<String,Object>();
            param.put("memberId", memberId);
            param.put("isDeleted", SundayCommonConstants.NO);
            param.put("isDefault",SundayCommonConstants.YES);
            //查询默认的收货地址,变更其状态
            List<SundayAddress> defaultAddress = super.list(param);
            if(ListUtil.isNotNull(defaultAddress)){
                for(SundayAddress add : defaultAddress){
                    super.update(add.getId(),"isDefault",0);
                }
            }
            //新增的都是默认地址
            SundayAddress address=new SundayAddress(
                    id,memberId, receiveName,receiveMobile,
                    provinceId, provinceName,cityId,cityName,districtId,districtName,
                    receiveAddress, 1);
            super.save(address);
        }else{
            super.updateExceptEmpty(id,new SundayAddress(id,memberId,receiveName,receiveMobile,provinceId, provinceName,cityId,cityName,districtId,districtName,receiveAddress, null));
        }
    }

    /**
     * 设置默认地址
     * @param addressId
     * @param memberId
     */
    public void setDefault(Long addressId, Long memberId){
        Map<String, Object> param =new HashMap<String,Object>();
        param.put("isDeleted", SundayCommonConstants.NO);
        param.put("memberId",memberId);
        List<SundayAddress> addresses = super.list(param);
        for(SundayAddress address:addresses){
            if(address.getId()==addressId){
                address.setIsDefault(SundayCommonConstants.YES);
                super.updateExceptEmpty(address.getId(),address);
            }else{
                address.setIsDefault(SundayCommonConstants.NO);
                super.updateExceptEmpty(address.getId(),address);
            }
        }
    }
    /**
     * 删除收货地址
     * @param addressId
     * @param memberId
     */
    public void delete(Long addressId, Long memberId){
        super.update(addressId,"isDeleted", SundayCommonConstants.YES);
    }

}
